var url = "WS://198.16.102.75:19002";

function Login(account, password) {
	var request = new proto.Req;
	request.setReqType(proto.RequestType.REQ_LOGIN);
	var reqLogin = new proto.ReqLogin;
	reqLogin.setAccount(account);
	reqLogin.setPassword(password);
	request.setReqLogin(reqLogin);

	if (WS != null) {
		WS.send(request.serializeBinary());
	}
}

function removeStrategy(name) {
	var request = new proto.Req;
	request.setReqType(proto.RequestType.REQ_REMOVE_STRATEGY);
	var s = new proto.ReqRemoveStrategy;
	var lst = [];
	lst.push(name);
	s.setStrategyList(lst);
	request.setReqRemoveStrategy(s);
	if (WS != null) {
		WS.send(request.serializeBinary());
		AddActionLog("已删除策略: " + name);
		removeStrategyNode(name);
	}
}

function removeSchema(name) {
	var request = new proto.Req;
	request.setReqType(proto.RequestType.REQ_REMOVE_SCENE);
	var s = new proto.ReqRemoveScene;
	var lst = [];
	lst.push(name);
	s.setNameList(lst);
	request.setReqRemoveScene(s);
	if (WS != null) {
		WS.send(request.serializeBinary());
		AddActionLog("申请删除场景: " + name);
		removeSchemaNode(name);
	}
}

function SaveCode(name, code) {
	var request = new proto.Req;
	request.setReqType(proto.RequestType.REQ_UPLOAD_FILE);
	var s = new proto.ReqUploadFile;
	s.setFileName(name);
	s.setContent(code);
	request.setReqUploadFile(s);
	if (WS != null) {
		WS.send(request.serializeBinary());
	}
}

function ReqCode(name) {
	var request = new proto.Req;
	request.setReqType(proto.RequestType.REQ_STRATEGY_DETAIL);
	var s = new proto.ReqStrategyDetail;
	s.setStrategy(name);
	request.setReqStrategyDetail(s);
	if (WS != null) {
		WS.send(request.serializeBinary());
	}
}

function popStrategyList(arr) {
	if (arr == null || arr.length < 1) {
		return;
	}
	arr = arr[0];
	var tree = [];
	for (var i = 0; i < arr.length; i++) {
		tree.push({
			"id": arr[i],
			"parent": "#",
			"text": arr[i]
		})
	}
	STRATEGY = tree;
	showStrategyTree()
}

function popSchemaList(arr) {
	if (arr == null || arr.length < 1) {
		return;
	}
	arr = arr[0];
	var tree = [];
	for (var i = 0; i < arr.length; i++) {
		tree.push({
			"id": arr[i][0],
			"parent": "#",
			"text": arr[i][0]
		})
	}
	SCHEMA = tree;
	showSchemaTree();
}

function reqReportSummary(id) {
	var request = new proto.Req;
	request.setReqType(proto.RequestType.REQ_SUMMARY);
	var s = new proto.ReqSummary;
	s.setId(id);
	request.setReqSummary(s);
	if (WS != null) {
		WS.send(request.serializeBinary());
	}
}

function reqDailyIncome(id) {
	var request = new proto.Req;
	request.setReqType(proto.RequestType.REQ_PAGINATION);
	var rp = new proto.ReqPagination;
	rp.setPaginationType(proto.PaginationType.PT_DAILY_INCOMES);
	rp.setAmt(100000000);
	rp.setSkip(0);
	rp.setId(id);
	request.setReqPagination(rp);
	if (WS != null) {
		WS.send(request.serializeBinary());
	}
}

function reqLongIncome(id) {
	var request = new proto.Req;
	request.setReqType(proto.RequestType.REQ_PAGINATION);
	var rp = new proto.ReqPagination;
	rp.setPaginationType(proto.PaginationType.PT_PROFIT_OF_EACH_LONG_TRADE);
	rp.setAmt(100000000);
	rp.setSkip(0);
	rp.setId(id);
	request.setReqPagination(rp);
	if (WS != null) {
		WS.send(request.serializeBinary());
	}
}

function reqShortIncome(id) {
	var request = new proto.Req;
	request.setReqType(proto.RequestType.REQ_PAGINATION);
	var rp = new proto.ReqPagination;
	rp.setPaginationType(proto.PaginationType.PT_PROFIT_OF_EACH_SHORT_TRADE);
	rp.setAmt(100000000);
	rp.setSkip(0);
	rp.setId(id);
	request.setReqPagination(rp);
	if (WS != null) {
		WS.send(request.serializeBinary());
	}
}

function reqTransactionAmt(id) {
	var request = new proto.Req;
	request.setReqType(proto.RequestType.REQ_PAGINATION_AMT);
	var rp = new proto.ReqPaginationAmt;
	rp.setPaginationType(proto.PaginationType.PT_TRADE);
	rp.setId(id);
	request.setReqPaginationAmt(rp);
	if (WS != null) {
		WS.send(request.serializeBinary());
	}
}

function reqTransaction(id, skip, take) {
	var request = new proto.Req;
	request.setReqType(proto.RequestType.REQ_PAGINATION);
	var rp = new proto.ReqPagination;
	rp.setPaginationType(proto.PaginationType.PT_TRADE);
	rp.setAmt(take);
	rp.setSkip(skip);
	rp.setId(id);
	request.setReqPagination(rp);
	if (WS != null) {
		WS.send(request.serializeBinary());
	}
}

function reqAccountDetail(id) {
	var request = new proto.Req;
	request.setReqType(proto.RequestType.REQ_PAGINATION);
	var rp = new proto.ReqPagination;
	rp.setPaginationType(proto.PaginationType.PT_ACCOUNT_DETAIL);
	rp.setAmt(100000000);
	rp.setSkip(0);
	rp.setId(id);
	request.setReqPagination(rp);
	if (WS != null) {
		WS.send(request.serializeBinary());
	}
}


function freshTaskList(rsp) {
    TASKS=[];
	rsp = rsp.getTaskList().array[0]
	for (var i = 0; i < rsp.length; i++) {
		if (rsp[i][0] == null) {
			rsp[i][0] = 0;
		}
		AddTask(rsp[i][3] == 2, rsp[i][1], rsp[i][0]);
	}
}

function reqRunStrategy(strateg, schema) {
	var request = new proto.Req;
	request.setReqType(proto.RequestType.REQ_RUN_STRATEGY_FROM_SCENE);
	var s = new proto.ReqRunStrategyFromScene;
	s.setStrategy(strateg);
	s.setSceneName(schema);
	request.setReqRunStrategyFromScene(s);
	if (WS != null) {
		WS.send(request.serializeBinary());
	}
}

function connect() {
	if (WS == null) {
		try {
			WS = new WebSocket(url);
	
		} catch (e) {	
			showLoginDialog()
			var msg= e.toString();
			if(msg.indexOf('Failed to execute')>-1){
				alert('服务器连接失败，请联系jebin');
			}
			
			WS=null;
			
		}		
	}
	WS.onopen = function(e) {

	};

	WS.onerror = function(e) {
		alert("后台服务异常，请联系开发者");
		
		showLoginDialog()
	};

	WS.onclose = function(e) {
		showLoginDialog()
	};

	WS.onmessage = function(e) {
		let fileReader = new FileReader();
		fileReader.readAsArrayBuffer(e.data);
		fileReader.onload = function(event) {
			let arrayBuffer = fileReader.result;
			var rsp = proto.Rsp.deserializeBinary(arrayBuffer);
			// console.log(rsp.getRspType());
			switch (rsp.getRspType()) {
				case proto.ResponseType.ON_LOGIN:
					if (rsp.getRspLogin().getAccount() != null) {
						hideLoginDialog();
					}
					break;
				case proto.ResponseType.ON_PUSH_STRATEGY_LIST:
					var data = rsp.getStrategyList();
					if (data != null) {
						popStrategyList(data.array);
					}

					break;
				case proto.ResponseType.ON_PUSH_SCENE_LIST:
					var data = rsp.getSceneList();
					if (data != null) {
						popSchemaList(data.array);
					}
					break;
				case proto.ResponseType.ON_REMOVE_STRATEGY:
					// if (data.array != null && data.array.length > 1) {
					// 	$('#treeStrategy').jstree(true).delete_node(data.array[0]);
					// 	console.log(data);
					// }
					break;
				case proto.ResponseType.ON_PUSH_TASK_LIST:
					// TASK=rsp;
					freshTaskList(rsp);
					break;
				case proto.ResponseType.ON_PUSH_INSTRUMENT:
					freshInstrument(rsp);
					break;
				case proto.ResponseType.RSP_STRATEGY_DETAIL:
					var data = rsp.getRspStrategyDetail();
					if (data.array != null && data.array.length > 1) {
						ShowEditorByStrategy(data.array[0], data.array[1]);
					}
					break;
				case proto.ResponseType.RSP_PAGINATION_AMT:
					switch (rsp.getRspPaginationAmt().getPaginationType()) {
						case proto.PaginationType.PT_TRADE:
							initTransactionPage(rsp.getRspPaginationAmt().getAmt());
							break;
					}
					break;
				case proto.ResponseType.RSP_PAGINATION:
					// console.log("RSP_PAGINATION:"+rsp);
					switch (rsp.getRspPagination().getPaginationType()) {
						case proto.PaginationType.PT_TRADE:
							PPP = rsp;
							showTransactionPage(rsp.getRspPagination().getTradeList());
							break;
						case proto.PaginationType.PT_ACCOUNT_DETAIL:
							var lst = rsp.getRspPagination().getAccountDetailList();
							LoadAccountDetail(lst);
							break;
						case proto.PaginationType.PT_DAILY_INCOMES:
							LoadDailyInComes(rsp.getRspPagination().getDailyincomesList());
							break;
						case proto.PaginationType.PT_PROFIT_OF_EACH_LONG_TRADE:
							var dt = rsp.getRspPagination().getProfitofeachlongtradeList();
							if (dt.length > 0) {
								LoadLongInComes(dt);
							}
							break;
						case proto.PaginationType.PT_PROFIT_OF_EACH_SHORT_TRADE:
							var dt = rsp.getRspPagination().getProfitofeachshorttradeList();
							if (dt.length > 0) {
								LoadShortInComes(dt);
							}
							break;
						case proto.PaginationType.PT_SCENE:
							console.log("PT_SCENE");
							break;
					}
					break;
					
				case proto.ResponseType.RSP_SET_SCENE:
					var name=rsp.getRspSetScene().getName();
					SCHEMA.push( {id: name, parent: "#", text: name});
					showSchemaTree();
					break;
				case proto.ResponseType.RSP_SUMMARY:
					showSummary(rsp.getRspSummary().getSummary());
					break;
				case proto.ResponseType.ON_UPLOAD_FILE:
					var data = rsp.getRspUploadFile();
					AddActionLog("策略已存云端:" + data);
					break;
				case proto.ResponseType.ON_RTN_LOG:
					AddActionLog(rsp.getLog().getMessage());
					break;
				case proto.ResponseType.ON_BT_TASK:
					UpdateSingleTask(rsp.getBtTask());
					break;

			}

		};
	};

}
